<?php
/**
 * @author spenceryg
 * @date 2022/12/9
 * @time 7:49 下午
 * @focus love & life
 */


namespace app\auto\business;


use DOMDocument;

class TemplateBusiness
{

    public function import($html)
    {
        $html = preg_replace('/{([\/]?extend(.+)?)}/', '<${1}>', $html);
        $html = preg_replace('/{([\/]?block(.+)?)}/', '<${1}>', $html);
        $html = preg_replace('/>\s+</', '><', $html);
        $html = trim($html);
        $html = trim($html, '"');
        $html = preg_replace('/\s@(.*?)="/', ' v-on:${1}="', $html);
        $html = '<meta http-equiv="Content-Type" content="text/html;charset=utf-8">' . $html;

        $doc = new DOMDocument();
        $doc->loadHtml($html);
        $obj = $doc->documentElement;
        $tree = [];
        $this->getTree($obj, $tree, 0);

//        return $tree;

        unset($tree[0]);
        unset($tree[1]);
        $tree[2]['pid'] = 0;

        $map = [];
        $result = [];
//        $map[0] = $data['pid'];
        $map[0] = 0;
        $first = true;
        $i = 0;
        foreach ($tree as $item) {
            if (in_array($item['name'], ['body', 'html', 'meta'])) {
                continue;
            }
            if ($first) {
                $item['pid'] = 0;
                $first = false;
            }
            $save = $item;
            unset($save['id']);
            $save['pid'] = $map[$save['pid']];
            $save['template_id'] = $data['template_id'];
            $save['left_tag'] = '<';
            $save['right_tag'] = '>';
            if ($item['name'] == 'block' || $item['name'] == 'extend') {
                $save['left_tag'] = '{';
                $save['right_tag'] = '}';
            }
            $result[] = $save;
//            $id = $this->add($save);
            $id = $i;

            $map[$item['id']] = $id;
            $i ++;
        }

        return $result;
    }

    public function getTree($obj, &$tree, $pid = 0)
    {
        $length = $obj->childNodes->length;
        for ($i = 0; $i < $length; $i++) {
            $this_item = [];
            $this_item['name'] = $obj->childNodes->item($i)->tagName;
            $id = uniqid();
            $this_item['pid'] = $pid;
            $this_item['content'] = '';
            $this_item['id'] = $id;
            $this_item['attr'] = [];
            $this_item['item'] = $obj->childNodes->item($i);
            $attr_length = $obj->childNodes->item($i)->attributes->length;
            for ($j = 0; $j < $attr_length; $j++) {
                $attr = $obj->childNodes->item($i)->attributes->item($j);
                $this_item['attr'][] = ['key' => $attr->name, 'val' => $attr->value ];
                if ($attr->name == 'ref') {
                    $this_item['ref'] = $attr->value;
                }
            }
            if ($this_item['name'] == 'script') {
                if ($obj->childNodes->item($i)->firstChild) {
                    $this_item['content'] = $obj->childNodes->item($i)->firstChild->data;
                }
            }
            if ($this_item['name'] == '') {
                $this_item['content'] = $obj->childNodes->item($i)->data;
            }
            $tree[] = $this_item;
            if ($obj->childNodes->item($i)->childNodes->length && $this_item['name'] != 'script') {
                $this->getTree($obj->childNodes->item($i), $tree, $id);
            }

        }
    }
}
